package com.yy.wga.mp.module.user.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.github.pagehelper.PageHelper;
import com.yy.wga.mp.module.user.entity.pojo.UserBaseDO;
import com.yy.wga.mp.module.user.entity.pojo.UserRelationDO;
import com.yy.wga.mp.module.user.entity.pojo.UserRelationSummaryDO;
import com.yy.wga.mp.module.user.mapper.UserRelationSummaryMapper;
import com.yy.wga.mp.module.user.service.UserBaseService;
import com.yy.wga.mp.module.user.service.UserRelationService;
import com.yy.wga.mp.module.user.service.UserRelationSummaryService;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;
import org.springframework.util.CollectionUtils;

import java.util.ArrayList;
import java.util.Collections;
import java.util.List;

/**
 * 用户粉丝数据汇总(UserRelationSummary)表服务实现类
 *
 * @author guochao
 * @since 2025-01-04 18:34:28
 */
@Slf4j
@Service
@RequiredArgsConstructor
public class UserRelationSummaryServiceImpl extends ServiceImpl<UserRelationSummaryMapper, UserRelationSummaryDO> implements UserRelationSummaryService {
    private final UserRelationService userRelationService;
    private final UserBaseService userBaseService;

    @Override
    public void summary() {
        Long minId = 0L;
        while(true) {
            PageHelper.startPage(1, 10, false);
            List<UserBaseDO> userBaseList = userBaseService.list(new LambdaQueryWrapper<UserBaseDO>()
                    .gt(UserBaseDO::getId, minId)
                    .orderByAsc(UserBaseDO::getId));
            if (CollectionUtils.isEmpty(userBaseList)) {
                return;
            }
            for (UserBaseDO userBase : userBaseList) {
                List<UserRelationDO> subRelationList = getSubRelationList(userBase.getId(), userBase.getAppKey());
                UserRelationSummaryDO userRelationSummary = baseMapper.selectOne(new LambdaQueryWrapper<UserRelationSummaryDO>()
                        .eq(UserRelationSummaryDO::getAppKey, userBase.getAppKey())
                        .eq(UserRelationSummaryDO::getUserId, userBase.getId()));
                if (userRelationSummary == null) {
                    userRelationSummary = new UserRelationSummaryDO();
                    userRelationSummary.setAppKey(userBase.getAppKey());
                    userRelationSummary.setUserId(userBase.getId());
                }
                userRelationSummary.setSubCount(subRelationList.size());
                this.saveOrUpdate(userRelationSummary);
                log.info("汇总用户{}粉丝数据", userRelationSummary.getUserId());
            }
            minId = userBaseList.get(userBaseList.size() - 1).getId();
        }
    }

    /**
     * 递归获取下级粉丝数据
     * @param userId 上级用户id
     * @param appKey appKey
     * @return 下级粉丝数据
     */
    private List<UserRelationDO> getSubRelationList(Long userId, String appKey) {
        List<UserRelationDO> allUserRelationList = new ArrayList<>();
        List<UserRelationDO> userRelationList = userRelationService.list(new LambdaQueryWrapper<UserRelationDO>()
                .eq(UserRelationDO::getAppKey, appKey)
                .eq(UserRelationDO::getParentUserId, userId));
        if (CollectionUtils.isEmpty(userRelationList)) {
            return allUserRelationList;
        }
        allUserRelationList.addAll(userRelationList);
        for (UserRelationDO userRelation : userRelationList) {
            allUserRelationList.addAll(this.getSubRelationList(userRelation.getUserId(), appKey));
        }
        return allUserRelationList;
    }
}
